﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcess;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcess;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProcessNodeService))]
    internal class ProcessNodeService : IProcessNodeService
    {
        [Import]
        protected IProcessRepository ProcessRepository { get; set; }
        [Import]
        protected IProcessNodeRepository ProcessNodeRepository { get; set; }

        public List<ProcessNodeView> QueryProcessNode(string processCode)
        {
            List<ProcessNodeView> processNodeViewList = new List<ProcessNodeView>();
            IQueryable<Process> processList = ProcessRepository.Entities.Where(p => p.ProcessCode == processCode);
            if (processList.Count() == 1)
            {
                Process process = processList.First();
                IQueryable<ProcessNode> processNodeList = ProcessNodeRepository.Entities.Where(p => p.PID == process.ID).OrderBy(p=>p.OrderNo);
                foreach (ProcessNode processNode in processNodeList)
                {
                    processNodeViewList.Add(new ProcessNodeView()
                    {
                        ID = processNode.ID,
                        PID = processNode.PID,
                        NodeCode = processNode.NodeCode,
                        NodeName = processNode.NodeName,
                        NodeType = processNode.NodeType,
                        DefaultProcessPerson = processNode.DefaultProcessPerson,
                        ProcessPersonCondition = processNode.ProcessPersonCondition,
                        PreviousNodeCode = processNode.PreviousNodeCode,
                        NextNodeCode = processNode.NextNodeCode,
                        OrderNo=processNode.OrderNo,
                        LocationX=processNode.LocationX,
                        LocationY=processNode.LocationY,
                        Width=processNode.Width,
                        Height=processNode.Height,
                        Shape=processNode.Shape,
                        NextNodeXML=processNode.NextNodeXML
                    });
                }
            }
            return processNodeViewList;
        }

        public List<ProcessNodeView> QuerySpecProcessNode(string processCode,bool isRequestNode,string nodeCode="",string nodeType="1")
        {
            List<ProcessNodeView> processNodeViewList = new List<ProcessNodeView>();
            IQueryable<Process> processList = ProcessRepository.Entities.Where(p => p.ProcessCode == processCode);
            if (processList.Count() == 1)
            {
                Process process = processList.First();
                IQueryable<ProcessNode> processNodeList ;
                if (isRequestNode)
                {
                    processNodeList = ProcessNodeRepository.Entities.Where(p => p.PID == process.ID && p.NodeType == nodeType);
                }
                else
                {
                    processNodeList = ProcessNodeRepository.Entities.Where(p => p.PID == process.ID && p.NodeCode == nodeCode);
                }
                foreach (ProcessNode processNode in processNodeList)
                {
                    processNodeViewList.Add(new ProcessNodeView()
                    {
                        ID = processNode.ID,
                        PID = processNode.PID,
                        NodeCode = processNode.NodeCode,
                        NodeName = processNode.NodeName,
                        NodeType = processNode.NodeType,
                        DefaultProcessPerson = processNode.DefaultProcessPerson,
                        ProcessPersonCondition = processNode.ProcessPersonCondition,
                        PreviousNodeCode = processNode.PreviousNodeCode,
                        NextNodeCode = processNode.NextNodeCode,
                        OrderNo = processNode.OrderNo,
                        LocationX = processNode.LocationX,
                        LocationY = processNode.LocationY,
                        Width = processNode.Width,
                        Height = processNode.Height,
                        Shape = processNode.Shape,
                        NextNodeXML = processNode.NextNodeXML
                    });
                }
            }
            return processNodeViewList;
        }
    }
}